//
//  DDTextField.m
//  hnGovernment
//
//  Created by 张辉男 on 17/3/8.
//  Copyright © 2017年 zhn. All rights reserved.
//

#import "DDTextField.h"

@interface DDTextField()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *field;
@property (nonatomic,strong) UIButton *secureTextButton;
@property (nonatomic,assign) BOOL isSecureText;
@end


@implementation DDTextField
#pragma mark - life cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.field];
        [self addSubview:self.secureTextButton];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect fieldBounds = CGRectMake(0, 0, self.bounds.size.width - (self.contentInset.left + self.contentInset.right), self.bounds.size.height - (self.contentInset.top + self.contentInset.bottom));
    CGRect fieldFrame = CGRectInset(fieldBounds, self.contentInset.left, self.contentInset.top);
    self.field.frame = fieldFrame;
    self.secureTextButton.frame = CGRectMake(self.bounds.size.width - 12 - 20, (self.bounds.size.height - 12)/2, 20, 12);
    [self.field addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
}

#pragma mark - public methods
+ (DDTextField *)ddTextFieldWithPlaceHolder:(NSString *)placeHolder Font:(CGFloat)font textColor:(UIColor *)textColor contentInset:(UIEdgeInsets)contentInset textChangingAction:(actionBlock)changingAction {
    DDTextField *ddtextField = [[DDTextField alloc]init];
    ddtextField.font = font;
    ddtextField.textColor = textColor;
    ddtextField.contentInset = contentInset;
    ddtextField.changingAction = changingAction;
    ddtextField.placeHolderString = placeHolder;
    return ddtextField;
}

#pragma mark - target action
- (void)textChanged:(UITextField *)textField {
    _text = textField.text;
    if (self.changingAction) {
        self.changingAction(self.field);
    }
    
    if ([self.delegate respondsToSelector:@selector(ddtextFieldChangingText:)]) {
        [self.delegate ddtextFieldChangingText:self];
    }
}

- (void)secureTextStateChange {
    if (!self.isNeedChangeSecureTextEntry) {return;}
    
    if (self.isSecureText) {
        self.field.secureTextEntry = NO;
        [self.secureTextButton setImage:[UIImage imageNamed:self.cryptographImageName] forState:UIControlStateNormal];
    }else {
        self.field.secureTextEntry = YES;
        [self.field becomeFirstResponder];
        [self.secureTextButton setImage:[UIImage imageNamed:self.proclaimedImageName] forState:UIControlStateNormal];
    }
    self.isSecureText = !self.isSecureText;
}

#pragma mark - pravite methods




#pragma mark - delegates



#pragma mark - setters
- (void)setPlaceHolderString:(NSString *)placeHolderString {
    _placeHolderString = placeHolderString;
    self.field.placeholder = placeHolderString;
}

- (void)setIsPassword:(BOOL)isPassword {
    _isPassword = isPassword;
    self.field.secureTextEntry = isPassword;
}

- (void)setFont:(CGFloat)font {
    _font = font;
    self.field.font = [UIFont systemFontOfSize:font];
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    self.field.textColor = textColor;
}

- (void)setShowClearButton:(BOOL)showClearButton {
    _showClearButton = showClearButton;
    if (showClearButton) {
       self.field.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
}

- (void)setProclaimedImageName:(NSString *)proclaimedImageName {
    _proclaimedImageName = proclaimedImageName;
    [self.secureTextButton setImage:[UIImage imageNamed:proclaimedImageName] forState:UIControlStateNormal];
}

- (void)setIsNeedChangeSecureTextEntry:(BOOL)isNeedChangeSecureTextEntry {
    _isNeedChangeSecureTextEntry = isNeedChangeSecureTextEntry;
    self.isSecureText = isNeedChangeSecureTextEntry;
    self.field.secureTextEntry = isNeedChangeSecureTextEntry;
    self.secureTextButton.hidden = NO;
}

#pragma mark - getters
- (UITextField *)field {
    if (_field == nil) {
        _field = [[UITextField alloc]init];
        _field.backgroundColor = [UIColor clearColor];
        _field.delegate = self;
        _field.clipsToBounds = YES;
    }
    return _field;
}
- (UIButton *)secureTextButton {
    if (_secureTextButton == nil) {
        _secureTextButton = [[UIButton alloc]init];
        _secureTextButton.hidden = YES;
        [_secureTextButton addTarget:self action:@selector(secureTextStateChange) forControlEvents:UIControlEventTouchUpInside];
    }
    return _secureTextButton;
}

- (NSString *)text {
    return _field.text;
}
@end
